package ui.activity.mine;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Bean.FindAddressBean;
import Utils.ScreenUtil;
import base.BaseAct;
import butterknife.BindView;
import coom.Constants;
import event.NetErrorEvent;
import ui.activity.mine.biz.NewAddressBiz;
import ui.activity.mine.component.DaggerNewAddressComponent;
import ui.activity.mine.contract.NewAddressContract;
import ui.activity.mine.module.NewAddressModule;
import ui.activity.mine.presenter.NewAddressPresenter;
import util.StringUtils;
import widget.CitySelectView;


/**
 * 登录界面
 * Created by chenjian on 2017/8/4.
 */

public class NewAddressAct extends BaseAct implements NewAddressContract.View {


    @Inject
    NewAddressPresenter presenter;
    @Inject
    NewAddressBiz biz;

    @BindView(R.id.edit_newaddress_name)
    EditText name;
    @BindView(R.id.edit_newaddress_phone)
    EditText phone;
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.newaddress_linear)
    RelativeLayout addressLinear;
    @BindView(R.id.edit_newaddress_address)
    EditText inputAddress;
    @BindView(R.id.back)
    LinearLayout back;

    @BindView(R.id.newaddress_submission)
    Button submit;
    @BindView(R.id.city_select_view)
    CitySelectView citySelectView;

    private String mAddressCode;
    private SharedPreferences sp;
    FindAddressBean.DataBean.ItemsBean bean;

    @Override
    protected void setRoot() {
        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_new_address);
    }

    @Override
    protected void setup() {
        DaggerNewAddressComponent.builder().newAddressModule(new NewAddressModule(this)).build().inject(this);
//        setToolBar(getResources().getString(R.string.lang_add_address), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        bean = (FindAddressBean.DataBean.ItemsBean) getIntent().getSerializableExtra(Constants.KEY);
        if (bean != null) {
            intData();
        }
        citySelectView.setAddressSelectedListener((code, address) -> {
            mAddressCode = code;
            addressTv.setText(address);
            addressTv.setTextColor(getResources().getColor(R.color.tc_333333));
            citySelectView.setVisibility(View.GONE);
        });

        RxView.clicks(back)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                  finishAct();
                });
        RxView.clicks(addressLinear)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    ScreenUtil.hideSoftInput(addressTv);
                    citySelectView.setVisibility(View.VISIBLE);
                });

        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    if (bean == null) {
                        presenter.addAddress(sp.getString("id", "0"), name.getText().toString().trim(), phone.getText().toString().trim(), inputAddress.getText().toString().trim(), mAddressCode);
                    } else {
                        presenter.editAddress(bean.getId(), name.getText().toString().trim(), phone.getText().toString().trim(), inputAddress.getText().toString().trim(), mAddressCode);

                    }
                });
    }

    private void intData() {
        String[] aa = bean.getAddress().split("-");
        String cityInfo = "";
        String addressInfo = "";
        if (aa.length > 0) {
            addressInfo = aa[aa.length - 1];
        }

        if (aa.length > 2) {
            for (int i = 0; i < aa.length - 1; i++) {
                cityInfo = cityInfo + aa[i] + "-";
            }
        }

        if (cityInfo.length() > 0) {
            cityInfo = cityInfo.substring(0, cityInfo.length() - 1);
        }
        name.setText(bean.getName());
        phone.setText(bean.getPhone());
        inputAddress.setText(addressInfo);
        addressTv.setText(cityInfo);
        if (!StringUtils.isEmpty(bean.getState())) {
            mAddressCode = mAddressCode + bean.getState() + " ";
        }
        if (!StringUtils.isEmpty(bean.getCity())) {
            mAddressCode = mAddressCode + bean.getCity() + " ";
        }
        if (!StringUtils.isEmpty(bean.getDistrict())) {
            mAddressCode = mAddressCode + bean.getDistrict();
        }
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


}
